<?php

class core
{
    public static $_Loaded;

    function __construct()
    {

    }

    function initialize()
    {
        set_include_path(get_include_path().PATH_SEPARATOR.DIR);
        set_include_path(get_include_path().PATH_SEPARATOR.LIBS_DIR);
        set_include_path(get_include_path().PATH_SEPARATOR.LIBS_DIR.'core');
        set_include_path(get_include_path().PATH_SEPARATOR.LIBS_DIR.'ui');
        set_include_path(get_include_path().PATH_SEPARATOR.DIR.'modules');
        set_include_path(get_include_path().PATH_SEPARATOR.DIR.'modules'.DIR_SEP.'all');
        set_include_path(get_include_path().PATH_SEPARATOR.DIR.'modules'.DIR_SEP.'all'.DIR_SEP.'components');
        set_include_path(get_include_path().PATH_SEPARATOR.DIR.'modules'.DIR_SEP.'all'.DIR_SEP.'helpers');
    }

    public static function load($object, $var = false)
    {
        //$error = new error();
        $valid = array(
                'router',
                'config',
                'controller',
                'request',
                'components',
                'helpers',
                'logs',
                'view',
                'compiler'
        );

        if (!in_array($object,$valid))
        {
            error::getInstance(E_ERROR ,'Erreur de framework ('.$object.')', __FILE__, __LINE__);
        }

        if (empty(self::$_Loaded[$object])){
            self::$_Loaded[$object]= new $object($var);
        }

        return self::$_Loaded[$object];

    }

    function dispatch($router)
    {
        global $app;

        ob_start();
        $config = self::load('config');

        $module = $router->getController();
        $controller = $router->getController();
        $action = $router->getAction();
        $requests = $router->getRequests();
        echo $module;

        if ($router->getRoutingType() == 2)
        {
            if (file_exists(DIR.'modules'.DIR_SEP.$module.DIR_SEP.'admin_'.$controller.'_controller.php'))
            {
                $controllerFile = DIR.'modules'.DIR_SEP.$module.DIR_SEP.'admin_'.$controller.'_controller.php';
            }
            else
            {
                error::getInstance(E_ERROR ,'Admin routing class not found', __FILE__, __LINE__);
            }
        }
        else
        {
            $controllerFile = DIR.'modules'.DIR_SEP.$module.DIR_SEP.$controller.'_controller.php';
        }

        if (!file_exists($controllerFile))
        {
            error::getInstance(E_ERROR ,'page not found', __FILE__, __LINE__);
        }

        include($controllerFile);

        if ($router->getRoutingType() == 2)
        {
            $controller1 = 'admin_'.$controller;
            $app = new $controller1();
        }
        else
        {
            $controller2 = 'normal_'.$controller;
            $app = new $controller2();
        }

        $app->setRequests($requests);

        if (method_exists($app, $action))
        {
            $app->$action();
        }
        else
        {
            error::getInstance(E_ERROR ,'action not found', __FILE__, __LINE__);

        }
        $routput = ob_get_clean();

        ob_start();

        $view = self::load("view");
        $viewVariables = $view->getVars($app);
        $layout = $app->layout;
        if (!$app->layout)
        {
            $layout = 'default';
        }

        $template = $view->getTemplate($action);

        if ($app->use_view == true)
        {
            if ($router->getRoutingType() == 2)
            {
                $view->loadTemplate($controller, $template, $viewVariables, $layout, true);

            }
            else
            {
                $view->loadTemplate($controller, $template, $viewVariables, $layout, false);
            }
        }
        else
        {
            echo $routput;
        }
        $output = ob_get_clean();
        echo $output;


    }

}

?>
